﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

namespace CB.Client
{
    public enum LayerType
    {
        Bottom,
        General,
        Top
    }

    /// <summary>
    /// 界面管理器
    /// </summary>
    public class UIManager
    {
        public static UIManager Instance = new UIManager();
        
        private Transform BottomCanvas;
        private Transform UICanvas;
        private Transform TopCanvas;

        // 存打开过的界面用的容器
        private Dictionary<string, UIContent> UIContener = new Dictionary<string, UIContent>();
        // prefab的路径前缀
        private string Path = "Prefabs/UIContent/";

        public void Init(Transform bot, Transform gen , Transform top)
        {
            BottomCanvas = bot;
            UICanvas = gen;
            TopCanvas = top;
        }

        // 显示一个界面, 受管理器管理, 同时只会有一个, 没有的话会创建并管理.
        public T ShowUIContent<T>(string id, LayerType layerType = LayerType.General, Transform parent = null) where T : UIContent
        {
            if (parent == null)
            {
                switch (layerType)
                {
                    case LayerType.Bottom:
                        parent = BottomCanvas;
                        break;
                    case LayerType.General:
                        parent = UICanvas;
                        break;
                    case LayerType.Top:
                        parent = TopCanvas;
                        break;
                    default:
                        break;
                }
            }

            if (UIContener.TryGetValue(id, out UIContent ui))
            {
                ui.gameObject.SetActive(true);
                InitContent(ui, parent);
            }
            else
            {
                ui = LoadUIContent<T>(id, parent);
                UIContener[id] = ui;
            }

            ui.transform.SetAsLastSibling();

            return ui as T;
        }

        // 一个不会受管理器管理的界面内容
        public T LoadUIContent<T>(string id, Transform parent = null) where T : UIContent
        {
            GameObject prefab = Resources.Load<GameObject>(Path + id);
            if (prefab?.GetComponent<T>() == null)
                return null;

            GameObject go = UnityEngine.Object.Instantiate(prefab);
            var ui = go.GetComponent<T>();
            if (ui == null)
                return null;

            if (parent == null)
            {
                parent = UICanvas;
            }
            go.SetActive(true);
            InitContent(ui, parent);

            return ui;
        }
        
        // 将prefab设置为标准状态
        private void InitContent(UIContent UI, Transform parent)
        {
            Transform trans = UI.transform;
            trans.SetParent(parent);
            trans.localScale = Vector3.one;
            trans.localPosition = Vector3.zero;
            trans.localRotation = Quaternion.identity;

            if (parent == UICanvas || parent == TopCanvas || parent == BottomCanvas)
            {
                RectTransform rt = trans.GetComponent<RectTransform>();
                rt.anchoredPosition = Vector2.zero;
                rt.sizeDelta = Vector2.zero;
            }
        }
    }
}
